<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

	public function index()
	{
		$this->load->view('login');
    }

	public function signin()
    {
        $this->load->library('form_validation');

        $config = array(
            array(
                'field' => 'username',
                'label' => '管理员账号',
                'rules' => 'trim|required|alpha_numeric|min_length[4]|max_length[8]|xss_clean',
            ),
            array(
                'field' => 'password',
                'label' => '密码',
                'rules' => 'trim|required|min_length[6]|max_length[12]|xss_clean',
            ),
            array(
                'field' => 'captcha',
                'label' => '验证码',
                'rules' => 'trim|required|alpha_numeric|min_length[8]|max_length[8]|xss_clean',
            ),
        );
        $this->form_validation->set_rules($config);

        if ($this->form_validation->run() === FALSE) {
            $error = $this->form_validation->error_array();
            $this->_error_handler($error);
            return FALSE;
        }

        $captcha = $this->session->userdata('captcha');

        if ($captcha === FALSE || !isset($captcha['expiration']) || !isset($captcha['word'])) {
            $error = array(
                'captcha' => '验证码缺失',
            );
            $this->_error_handler($error);
            return FALSE;
        }

        $is_expired = isset($captcha['expiration']) && !empty($captcha['expiration']) && $captcha['expiration'] >= time();
        if (!$is_expired) {
            $error = array(
                'captcha' => '验证码已过期',
            );
            $this->_error_handler($error);
            return FALSE;
        }

        if ($this->input->post('captcha') != $captcha['word']) {
            $error = array(
                'captcha' => '输入的验证码不符',
            );
            $this->_error_handler($error);
            return FALSE;
        }

        $this->load->model('Admin_model', 'admin');
        $user = $this->admin->get_admin_by_name($this->input->post('username'));
        if ($user === FALSE) {
            $error = array(
                'username' => '错误的用户名或密码',
            );
            $this->_error_handler($error);
            return FALSE;
        }

        $is_matche = $user['passwd'] == md5(md5($this->input->post('username')) + $user['salt']);
        if ($is_matche) {
            $this->load->helper('url');
            redirect('/home/index');
        }
        else {
            $error = array(
                'password' => '错误的用户名或密码',
            );
            $this->_error_handler($error);
        }
    }


    private function _error_handler($error)
    {
        $data = array(
            'error' => $error,
        );
        $this->load->view('login', $data);

    }

	public function captcha()
    {
        $this->load->helper('captcha');
        $this->load->helper('url');

        $config = array(
            'img_path' => APPPATH . '../assets/captcha/',
            'img_url'  => base_url() . 'assets/captcha/',
            'img_width' => '100',
            'img_height' => 30,
            'expiration' => 7200
            );

        $captcha = create_captcha($config);

        $this->session->set_userdata(
            array(
                'captcha' => array(
                    'word'          => $captcha['word'],
                    'expiration'    => $config['expiration'],
                )
            )
        );

        $img_path = "./{$config['img_path']}{$captcha['time']}.jpg";

        if ( file_exists($img_path) === TRUE ) {
            $this->output->set_content_type('jpeg')
                ->set_output(file_get_contents($img_path));
        }
        else {
            log_message('error', "{$img_path}文件不存在，验证码生成失败");
        }
	}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */
